var config = require('./config.js')
var app = getApp();

function PostAxis(reqObj) {
  wx.request({
    url: config.baseUrl + reqObj.url,
    data: reqObj.data,
    method: 'POST',
    header: {
      "Content-Type": "application/json"
    },
    success: function (res) {
      console.log(reqObj.command + "接收数据：" + JSON.stringify(res.data));
      reqObj.success(res.data);
    },
    fail: function (res) {
      console.log(reqObj.command + "获取失败," + JSON.stringify(res));
      wx.showToast({
        icon: 'none',
        title: '请求服务超时',
        duration: config.toast.duration
      })
      reqObj.fail(res);
    }
  })
}

function GetAxis(reqObj) {
  wx.request({
    url: config.baseUrl + reqObj.url,
    data: reqObj.data,
    method: 'Get',
    header: {
      "Content-Type": "application/json"
    },
    success: function (res) {
      console.log(reqObj.command + "接收数据：" + JSON.stringify(res.data));
      reqObj.success(res.data);
    },
    fail: function (res) {
      console.log(reqObj.command + "获取失败," + JSON.stringify(res));
      wx.showToast({
        icon: 'none',
        title: '请求服务超时',
        duration: config.toast.duration
      })
      reqObj.fail(res);
    }
  })
}
let canShowModal = true;
// let ajaxTime = 0;
const Request = (options) => {
  // ajaxTime++;
  // wx.showLoading({
  //   title: '加载中',
  //   mask: true
  // })
  return new Promise((resolve, reject) => {
    const token = wx.getStorageSync(config.storageKey.token)
    const xtoken = wx.getStorageSync(config.storageKey.xtoken)
    wx.request({
      url: config.baseUrl + options.url,
      data: options.data || {},
      method: options.method || 'POST',
      header: {
        "Content-Type": "application/json",
        'Authorization': 'Bearer ' + token,
        'X-Authorization': 'Bearer ' + xtoken
      },
      // responseType:options.responseType || "",
      timeout: 0,
      success(res) {
        if (res.statusCode === 200) {
          if (res.data.code === 200) {
            resolve(res.data);
          } else if (res.data.code == 401 || res.data.code == 410) {
            // token过期后调登录
            wx.request({
              url: config.baseUrl + '/api/SystemMoule/Account/WxLogin?openId=' + app.globalData.openid,
              method: 'POST',
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                app.globalData.token = res.data.data.accessToken;
                app.globalData.xtoken = res.data.data.refreshToken;
                wx.setStorageSync(config.storageKey.token, res.data.data.accessToken);
                wx.setStorageSync(config.storageKey.xtoken, res.data.data.refreshToken);
                // 登录成功，重新调之前的接口
                wx.request({
                  url: config.baseUrl + options.url,
                  data: options.data || {},
                  method: options.method || 'POST',
                  header: {
                    "Content-Type": "application/json",
                    'Authorization': 'Bearer ' + res.data.data.accessToken,
                    'X-Authorization': 'Bearer ' + res.data.data.refreshToken
                  },
                  success(resf) {
                    if (resf.data.code === 200) {
                      resolve(resf.data);
                    } else {
                      wx.showToast({
                        icon: 'none',
                        title: '请求失败',
                        duration: config.toast.duration
                      })
                      reject(resf);
                    }
                  },
                  fail(resf) {
                    wx.showToast({
                      icon: 'none',
                      title: '请求服务超时',
                      duration: config.toast.duration
                    })
                    reject(resf);
                  }
                })
              }
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.info,
              duration: config.toast.duration
            })
            reject(res);
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: '请求失败',
            duration: config.toast.duration
          })
          reject(res);
        }
      },
      fail(res) {
        wx.showToast({
          icon: 'none',
          title: '请求服务超时',
          duration: config.toast.duration
        })
        reject(res);
      },
      complete: () => {
        // ajaxTime--;
        // if (ajaxTime == 0) {
        //   wx.hideLoading();
        // }
      }
    })
  })
};

function CheckUser() {
  if (!canShowModal) return;
  canShowModal = false
  if (app.globalData.userInfo == null || app.globalData.userInfo == undefined) {
    wx.showModal({
      title: '',
      showCancel: false,
      content: '当前未绑定用户，请先绑定用户',
      success(res) {
        if (res.confirm) {
          canShowModal = true;
          wx.reLaunch({
            url: "../login/login"
          })
        } else {
          canShowModal = true;
        }
      }
    })
  }
}
module.exports = {
  PostAxis: PostAxis,
  GetAxis: GetAxis,
  Request: Request
}